package lu.med.praxiswahl.wahlumed.client.patienten;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;

import lu.med.praxiswahl.wahlumed.client.utilities.UniCode;
import lu.med.praxiswahl.wahlumed.client.wclient.Main;
import lu.med.praxiswahl.wahlumed.client.wclient.PatientenSuchmaske;
import lu.med.praxiswahl.wahlumed.client.wclient.PatientenSuchmaskeListener;


public class EGravPanel extends JPanel implements ActionListener,
	PatientenSuchmaskeListener
{
    /**
     * 
     */
    private static final long serialVersionUID = 7909853986016821927L;
    private PatientenSuchmaske search;
    private JPanel einblendenPanel;
    private EGravTableModel model;
    private String matricule = "";
    private Main dataManager;
    private JTabbedPane tabbedPane;

    public EGravPanel(Main dataManager, PatientenSuchmaske search,
	    JTabbedPane tabbedPane)
    {
	super(new BorderLayout());
	this.tabbedPane = tabbedPane;
	this.dataManager = dataManager;
	this.matricule = "";
	model = new EGravTableModel(dataManager);
	EGravTable table = new EGravTable(model);
	JScrollPane scp = new JScrollPane(table);
	this.add(scp, BorderLayout.CENTER);
	this.add(search, BorderLayout.EAST);
	this.search = search;
	search.addSuchmaskeListener(this);
	einblendenPanel = new JPanel(new BorderLayout());
	JButton einblendenButton = new JButton("Einblenden");
	einblendenButton.addActionListener(this);
	einblendenButton.setPreferredSize(new Dimension(40, 200));
	einblendenPanel.add(einblendenButton, BorderLayout.EAST);

	JPanel buttonPanel = new JPanel(new GridLayout(1, 0));
	JButton newDatasetButton = new JButton("Neuer Eintrag");
	JButton dropButton = new JButton("L" + UniCode.getoe() + "schen");
	JButton printButton = new JButton("Drucken");
	JButton curButton = new JButton("Aktualisieren");
	// JButton oBButton = new JButton("Ohne Test");
	// JButton datumButton = new JButton("Zeitraum");
	newDatasetButton.addActionListener(this);
	dropButton.addActionListener(this);
	printButton.addActionListener(this);
	curButton.addActionListener(this);
	// oBButton.addActionListener(this);
	// datumButton.addActionListener(this);
	buttonPanel.add(newDatasetButton);
	buttonPanel.add(dropButton);
	buttonPanel.add(printButton);
	buttonPanel.add(curButton);
	// JLabel label = new JLabel("Filter");
	// label.setFont(new Font("Times New Roman",Font.BOLD,24));
	// buttonPanel.add(label);
	// buttonPanel.add(oBButton);
	// buttonPanel.add(datumButton);
	this.add(buttonPanel, BorderLayout.NORTH);
    }

    public void actionPerformed(ActionEvent e)
    {
	if (e.getActionCommand().equals("Aktualisieren"))
	{
	    model.refresh();
	}
	if (e.getActionCommand().equals("Ohne Test"))
	{
	    model.transferEmptyDataFromDatabase();
	}
	if (e.getActionCommand().equals("Zeitraum"))
	{
	    // Datums Auswahl Dialog
	    // new GravTestDatumswahlDialog(model,dataManager);
	}
	if (e.getActionCommand().equals("Neuer Eintrag"))
	{
	    // Fügt dem Modell einen neuen Eintrag hinzu
	    if (matricule.equals(""))
	    {
		JOptionPane.showMessageDialog(dataManager, "Sie m"
			+ UniCode.getue() + "ssen zuerst eine Patientin ausw"
			+ UniCode.getae() + "hlen");
	    }
	    else
	    {
		model.addRow(matricule);
	    }
	}
	if (e.getActionCommand().equals("Ausblenden"))
	{
	    // Blendet das searchPanel aus und das Einblenden Panel ein
	    this.remove(search);
	    this.add(einblendenPanel, BorderLayout.EAST);
	    this.repaint();
	}
	if (e.getActionCommand().equals("Einblenden"))
	{
	    this.remove(einblendenPanel);
	    this.add(search, BorderLayout.EAST);
	    this.repaint();
	}
	if (e.getActionCommand().equals("L" + UniCode.getoe() + "schen"))
	{
	    model.dropCurrentLine();
	}
    }

    public void modelChanged(String matricule)
    {
	/*
	 * this.matricule = matricule; model.setMatricule(matricule);
	 */

	if (this.tabbedPane.getSelectedIndex() == 4)
	{
	    this.matricule = matricule;
	    model.setMatricule(matricule);
	}
    }
}
